/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.directedit;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Text;

/**
 * Locator of a cell editor for a figure.
 */
final public class SimpleCellEditorLocator implements CellEditorLocator {
	
	/**
	 * Label to be edited.
	 */
	private IFigure figureToEdit;

	/**
	 * Creates a new LabelCellEditorLocator.
	 * 
	 * @param figureToEdit The figure to be edited.
	 */
	public SimpleCellEditorLocator(IFigure figureToEdit) {
		this.figureToEdit = figureToEdit;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.tools.CellEditorLocator#relocate(org.eclipse.jface.viewers.CellEditor)
	 */
	/**
	 * 
	 * 
	 * @param celleditor 
	 */
	public void relocate(CellEditor celleditor) {
		Text text = (Text)celleditor.getControl();
		Point pref = text.computeSize(-1, -1);
		// min size for editor = 64
		pref.x = Math.max(pref.x, 64);
		Rectangle rect = figureToEdit.getClientArea().getCopy();
		figureToEdit.translateToAbsolute(rect);
		text.setBounds(rect.x-10, rect.y-1, pref.x+20, pref.y+1);	
	}
}